
package it.enrollement;


import it.user.User;
import it.utils.Hash;
import java.util.Random;
import jpair.pairing.BigInt;
import jpair.pairing.Point;

/**
 * The KGC generates secret (signature) keys for a set of users registered through their identities. 
 * 
 * @author Francesco Rossi
 * @version 0.1
 * 
 */
public class Extract {
    

   public Extract(){}
     /**
      * 
      * @param param System public paramenter generate with Setup
      * @param m The User to inizialize
      * @param ID Identity of user m
      */
     public void Init(System_Param param,User m,String ID){

         BigInt kid; /** BigInt kid;  private short term key used in IB_FG protocol */

         BigInt kbls; /** BigInt kbls; private key used for IB_BLS */
         

        Point Qid, Tid; /** Point Qid; user public value */

         BigInt kzss; /** BigInt kzss; private key used for IB_ZSS   */   
         
           BigInt kdsa,x; /** BigInt kdsa; private key used for IB_dsa   */   

         
         BigInt hID, hIDQid, rid, q,q2;
  
         Random rnd;
         Hash hash = new Hash();
    
      try{
          
       rnd = new Random();

          // rid random value in Zp
          rid = new BigInt(param.getE().getCurve().getField().getOrder().bitLength(), rnd);
   
          // Qid=ridP 
         Qid = param.getE().getCurve().multiply(param.getP(), rid);


          // h(ID)
          hID = hash.h1(ID.getBytes("UTF-8"), param.getE().getCurve().getField());

          // h(ID|Qid)

          hIDQid = hash.h1((ID + Qid.toString()).getBytes("UTF-8"), param.getE().getCurve().getField());

  

          // kbls = srh(ID)
          kbls = param.getR().multiply(hID.multiply(param.getS())).mod(param.getE().getGroupOrder());
          
       

          // kid = rid+ sh(ID|Qid)     
          kid = rid.add(param.getS().multiply(hIDQid)).mod(param.getE().getGroupOrder());;

                
          
             //h(ID)r +s
            q=(hID.multiply(param.getR())).add(param.getS());
   

          
          // 1/(h(id)r +s)
          kzss = q.modInverse(param.getE().getGroupOrder());

       
       rnd = new Random();
          // rid random value in Zp
          x = new BigInt(param.getE().getCurve().getField().getOrder().bitLength(), rnd);
          
                // dsa = sxh(ID)
      //    kdsa = x.multiply(hID.multiply(param.getS())).mod(param.getE().getGroupOrder());
          
          kdsa = x.multiply(hID.multiply(param.getS())).mod(param.getE().getGroupOrder());
       
            // Tid=xP 
         Tid = param.getE().getCurve().multiply(param.getP(), kdsa);

          m.setKzss(kzss);
          m.setKbls(kbls);
          m.setKid(kid);
          m.setQ(Qid);
          m.setKdsa(kdsa);
          m.setTid(Tid);
          
      }
       catch (Exception c)
         {   c.printStackTrace();
   
         }  
            
        
    }

}
